using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for SMS_DB.
	/// </summary>
	
	public class SMS_DB
	{
		private string _cnnString;
		public SMS_DB()
		{
			//
			cnnString=ConfigurationSettings.AppSettings["ConnStr"];
			//
		}

		public SMS_DB(string connStr)
		{
			cnnString=connStr;
		}
		
		public string cnnString
		{
			get
			{
				return _cnnString;
			}
			set
			{
				_cnnString = value;
			}
		}
		

		public DataSet GetData(string strSQL)
		{
			SqlConnection conn = new SqlConnection(cnnString);
			SqlDataAdapter oAdapter = new SqlDataAdapter(strSQL,conn);
			DataSet resultData = new DataSet();
			conn.Open();
			oAdapter.Fill(resultData);
			conn.Close();
			return resultData;
		}
		public DataSet GetData(string ConnStr,string strSQL)
		{
			SqlConnection conn = new SqlConnection(ConnStr);
			SqlDataAdapter oAdapter = new SqlDataAdapter(strSQL,conn);
			DataSet resultData = new DataSet();
			conn.Open();
			oAdapter.Fill(resultData);
			conn.Close();
			return resultData;
		}
		public void RunSQL(string ConnStr,string strSQL)
		{
			SqlConnection conn = new SqlConnection(ConnStr);
			SqlCommand mCommand = new SqlCommand(strSQL,conn);
			conn.Open();
			mCommand.ExecuteNonQuery();
			conn.Close();
		}
		public void RunSQL(string strSQL)
		{
			SqlConnection conn = new SqlConnection(cnnString);
			SqlCommand mCommand = new SqlCommand(strSQL,conn);
			conn.Open();
			mCommand.ExecuteNonQuery();
			conn.Close();
		}				
		#region Management Methods

		/// <summary>
		/// return an Open SqlConnection
		/// </summary>
		public SqlConnection OpenConnection(string connectionString)
		{
			try
			{
				SqlConnection mySqlConnection = new SqlConnection(connectionString);
				mySqlConnection.Open();
				return mySqlConnection;
			}
			catch(Exception myException)
			{
				throw(new Exception(myException.Message));
			}
		}
		/// <summary>
		/// return an Open SqlConnection
		/// </summary>
		public SqlConnection OpenConnection()
		{
			try
			{
				SqlConnection mySqlConnection = new SqlConnection(_cnnString);
				mySqlConnection.Open();
				return mySqlConnection;
			}
			catch(Exception myException)
			{
				throw(new Exception(myException.Message));
			}
		}

		/// <summary>
		/// close an SqlConnection
		/// </summary>
		public void CloseConnection(SqlConnection mySqlConnection)
		{
			try
			{
				if (mySqlConnection.State == ConnectionState.Open)
				{
					mySqlConnection.Close();
				}
			}
			catch(Exception myException)
			{
				throw(new Exception(myException.Message));
			}
		}

		/// <summary>
		/// return a DataSet from SQL Request
		/// </summary>
		public DataSet SQLtoDataSet(string strSQL)
		{
			SMS_DB DB = new SMS_DB();
			try
			{
				SqlConnection myConn = DB.OpenConnection(DB.cnnString);
				SqlDataAdapter myDataAdapter = new SqlDataAdapter (strSQL, myConn);
				DataSet myDataSet = new DataSet();
				myDataAdapter.Fill(myDataSet);
				DB.CloseConnection(myConn);
				return myDataSet;
			}
			catch(Exception myException)
			{
				throw(new Exception(myException.Message +" => " + strSQL));
			}
		}
		public void ExecuteSQL(string strSQL)
		{
			SMS_DB DB = new SMS_DB();
			try
			{
				SqlConnection myConn = DB.OpenConnection(cnnString);
				SqlCommand myCom= new SqlCommand(strSQL,myConn);
				myCom.ExecuteNonQuery();
				DB.CloseConnection(myConn);
			}
			catch(Exception myException)
			{
				throw(new Exception(myException.Message +" => " + strSQL));
			}

		}
		public void ExecuteSQL(string strSQL,SqlConnection mConn)
		{
			try
			{
				SqlCommand myCom= new SqlCommand(strSQL,mConn);
				myCom.ExecuteNonQuery();
			}
			catch(Exception myException)
			{
				throw(new Exception(myException.Message +" => " + strSQL));
			}

		}
		
		#endregion

	}
}
